/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import dao.DAOCuaHangProduct;
import dao.DAOListCuaHang;
import dao.DAOProduct;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import pojo.CuahangProduct;
import pojo.CuahangProductId;
import pojo.ListCuahang;
import pojo.Product;
import static view.QuyDinhTongKho.TryParseInt;

/**
 *
 * @author QTuan
 */
public class QuyDinhCuaHang extends javax.swing.JPanel {

    /**
     * Creates new form QuyDinhCuaHang
     */
    private DefaultTableModel model = new DefaultTableModel();
    public QuyDinhCuaHang() {
        initComponents();
        DAOListCuaHang daocuahang = new DAOListCuaHang();
        List<ListCuahang> cuahang = daocuahang.getAll();
        for(int i=0;i<cuahang.size();i++)
        {
            this.jComboBox1.addItem(cuahang.get(i).getName());
        }
        model.addColumn("Id");
        model.addColumn("Tên sản phẩm");
        model.addColumn("Số lượng tồn");
        model.addColumn("Tồn tối thiểu");
        model.addColumn("Tồn tối đa");
        this.jTable1.setModel(model);
        this.jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();

        jLabel1.setText("Thay đổi lượng hàng của cửa hàng");

        jLabel2.setText("Cửa hàng");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Tìm kiếm");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Edit");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel3.setText("Tồn tối thiểu");

        jLabel4.setText("Tồn tối đa");

        jButton3.setText("Thay đổi");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(63, 63, 63)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(59, 59, 59)
                                .addComponent(jButton1)))
                        .addContainerGap(536, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 566, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButton2)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel3)
                                            .addComponent(jLabel4))
                                        .addGap(58, 58, 58)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jTextField1)
                                            .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)))))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton3)
                                .addGap(39, 39, 39))))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jButton1))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(34, 34, 34)
                        .addComponent(jButton3)))
                .addContainerGap(45, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String cuahang = this.jComboBox1.getSelectedItem().toString();
        DAOListCuaHang daocuahang = new DAOListCuaHang();
        ListCuahang cuahang1 = daocuahang.getCuaHangByName(cuahang);
        DAOCuaHangProduct daocuahangprodut = new DAOCuaHangProduct();
        List<CuahangProduct> lis = daocuahangprodut.getCuaHangProduct(cuahang1.getId());
        for(int i = model.getRowCount()-1;i>=0;i++)
        {
            model.removeRow(0);
        }
        for(int i =0;i<lis.size();i++)
        {
            DAOProduct daoproduct = new DAOProduct();
            Product product = daoproduct.getProductById(lis.get(i).getId().getIdProduct());
            model.addRow(new Object[]{product.getId(),product.getProName(),lis.get(i).getCount(),lis.get(i).getToithieuL(),lis.get(i).getToidaK()});
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        int select = this.jTable1.getSelectedRow();
        if(select<0)
        {
            JOptionPane.showMessageDialog(null, "Bạn chưa chọn sản phẩm muốn thay đổi giá khuyến mãi","Chưa chọn",JOptionPane.WARNING_MESSAGE);
        }else
        {
            this.jTextField1.setText(model.getValueAt(select, 3).toString());
            this.jTextField2.setText(model.getValueAt(select, 4).toString());
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
         Integer toithieu = TryParseInt(this.jTextField1.getText());
        Integer toida = TryParseInt(this.jTextField2.getText());
        int select = this.jTable1.getSelectedRow();
        if(toida == null || toithieu == null)
        {
            JOptionPane.showMessageDialog(null, "Bạn phải nhập là dạng số","Lỗi nhập",JOptionPane.WARNING_MESSAGE);
        }else
        {
            if(toithieu>=toida)
            {
                JOptionPane.showMessageDialog(null, "Tối thiểu phai nhỏ hơn tối đa","Lỗi nhập",JOptionPane.WARNING_MESSAGE);
            }else
            {
                String cuahang = this.jComboBox1.getSelectedItem().toString();
                DAOListCuaHang daocuahang = new DAOListCuaHang();
                ListCuahang cuahang1 = daocuahang.getCuaHangByName(cuahang);
                DAOCuaHangProduct daocuahangproduct = new DAOCuaHangProduct();
                CuahangProduct cuahangproduc = new CuahangProduct();
                cuahangproduc.setId(new CuahangProductId(cuahang1.getId(), (Integer)model.getValueAt(select, 0)));
                cuahangproduc.setCount((Integer)model.getValueAt(select, 2));
                cuahangproduc.setToithieuL(toithieu);
                cuahangproduc.setToidaK(toida);
                daocuahangproduct.updateProduct(cuahangproduc);
            }
            
        }
    }//GEN-LAST:event_jButton3ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
